home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C ++ / Applications / Pict2Ascii 1.03 / Src / AS / CPict2AsciiApp.cp < prev    next >
Encoding:
Text File  |  1997-07-09  |  13.3 KB  |  466 lines  |  [TEXT/CWIE]

  1. // =================================================================================
  2. //    CPict2AsciiApp.cp                        1997 BB's Team inc. All rights reserved
  3. // =================================================================================
  4. #include "PLConstants.h"
  5. #include "CPreferences.h"
  6. #include "CDocument.h"
  7. #include "CPict2AsciiApp.h"
  8.  
  9. #include "CDynamicEditField.h"
  10. #include "CMemoryIndicator.h"
  11. #include "LDynamicPopupMenu.h"
  12. #include "CDynamicText.h"
  13. #include "CGreyCaption.h"
  14. #include "CGWorldPane.h"
  15.  
  16. // StandardGetFilePreview
  17. #include <ImageCompression.h>
  18.  
  19. #include <LGrowZone.h>
  20. #include <LMenu.h>
  21. #include <LMenuBar.h>
  22. #include <LStdControl.h>
  23. #include <LString.h>
  24. #include <UDesktop.h>
  25. #include <UDrawingState.h>
  26. #include <UMemoryMgr.h>
  27. #include <LDialogBox.h>
  28. #include <UReanimator.h>
  29. #include <URegistrar.h>
  30. #include <LEditField.h>
  31. #include <LPrintOut.h>
  32. #include <LRadioGroup.h>
  33. #include <LActiveScroller.h>
  34. #include <LGroupBox.h>
  35.  
  36. #include <PP_Messages.h>
  37.  
  38. #include <UAppleEventsMgr.h>    // MJS 97-05-29
  39. #include <UExtractFromAEDesc.h>    // MJS 97-05-29
  40.  
  41.  
  42. #ifdef PL_DEBUG
  43.     #include <LSIOUXAttachment.h>
  44. #endif
  45.  
  46. // =================================================================================
  47. //        • Main Program
  48. // =================================================================================
  49. void
  50. main( void )
  51. {
  52.     // Initialize the heap. Parameter is number
  53.     // of master handle blocks to allocate.
  54.     InitializeHeap ( 4 );
  55.     
  56.     // Initialize the MacOS toolbox.
  57.     UQDGlobals::InitializeToolbox( &qd );
  58.  
  59.     // Setup the throw and signal actions.
  60. #ifdef PL_DEBUG
  61.     SetDebugThrow_( debugAction_Alert );
  62.     SetDebugSignal_( debugAction_Alert );
  63. #endif
  64.  
  65.     // Install a GrowZone function to catch  low memory situations.
  66.     // Parameter is the size of the memory reserve in bytes.
  67.     new LGrowZone( 30000 );
  68.  
  69.     // Create the application object and run it.
  70.     CPict2AsciiApp    theApp;
  71.     theApp.Run();
  72. }
  73.  
  74.  
  75. // ---------------------------------------------------------------------------------
  76. //        • CPict2AsciiApp
  77. // ---------------------------------------------------------------------------------
  78. CPict2AsciiApp::CPict2AsciiApp()
  79. {
  80.     // Register PowerPlant class creator functions.
  81.     URegistrar::RegisterClass(LCaption::class_ID,        (ClassCreatorFunc) LCaption::CreateCaptionStream);
  82.     URegistrar::RegisterClass(LDialogBox::class_ID,        (ClassCreatorFunc) LDialogBox::CreateDialogBoxStream);
  83.     URegistrar::RegisterClass(LEditField::class_ID,        (ClassCreatorFunc) LEditField::CreateEditFieldStream);
  84.     URegistrar::RegisterClass(LPane::class_ID,            (ClassCreatorFunc) LPane::CreatePaneStream);
  85.     URegistrar::RegisterClass(LPlaceHolder::class_ID,    (ClassCreatorFunc) LPlaceHolder::CreatePlaceHolderStream);
  86.     URegistrar::RegisterClass(LPrintout::class_ID,        (ClassCreatorFunc) LPrintout::CreatePrintoutStream);
  87.     URegistrar::RegisterClass(LStdButton::class_ID,        (ClassCreatorFunc) LStdButton::CreateStdButtonStream);
  88.     URegistrar::RegisterClass(LStdCheckBox::class_ID,    (ClassCreatorFunc) LStdCheckBox::CreateStdCheckBoxStream);
  89.     URegistrar::RegisterClass(LStdRadioButton::class_ID,(ClassCreatorFunc) LStdRadioButton::CreateStdRadioButtonStream);
  90.     URegistrar::RegisterClass(LStdPopupMenu::class_ID,    (ClassCreatorFunc) LStdPopupMenu::CreateStdPopupMenuStream);
  91.     URegistrar::RegisterClass(LWindow::class_ID,        (ClassCreatorFunc) LWindow::CreateWindowStream);
  92.  
  93.     URegistrar::RegisterClass(LRadioGroup::class_ID,    (ClassCreatorFunc) LRadioGroup::CreateRadioGroupStream);
  94.     URegistrar::RegisterClass(LActiveScroller::class_ID,(ClassCreatorFunc) LActiveScroller::CreateActiveScrollerStream);
  95.     URegistrar::RegisterClass(LGroupBox::class_ID,        (ClassCreatorFunc) LGroupBox::CreateGroupBoxStream);
  96.  
  97.     // Register custom classes.
  98.     URegistrar::RegisterClass(     CDynamicText::class_ID, (ClassCreatorFunc)     CDynamicText::CreateDynamicTextStream );
  99.     URegistrar::RegisterClass(     CGreyCaption::class_ID, (ClassCreatorFunc)     CGreyCaption::CreateGreyCaptionStream );
  100.     URegistrar::RegisterClass(      CGWorldPane::class_ID, (ClassCreatorFunc)      CGWorldPane::CreateGWorldPaneStream  );
  101.     URegistrar::RegisterClass(LDynamicPopupMenu::class_ID, (ClassCreatorFunc) LDynamicPopupMenu::CreateLDynamicPopupMenuStream    );
  102.     URegistrar::RegisterClass( CMemoryIndicator::class_ID, (ClassCreatorFunc)  CMemoryIndicator::CreateMemoryIndicatorStream    );
  103.     URegistrar::RegisterClass(CDynamicEditField::class_ID, (ClassCreatorFunc) CDynamicEditField::CreateDynamicEditFieldStream    );
  104.  
  105. #ifdef PL_DEBUG
  106.     AddAttachment(new LSIOUXAttachment);    // *** Use SIOUX Attachment
  107. #endif
  108.     mPreferences.FinishCreate();
  109. }
  110.  
  111.  
  112. // ---------------------------------------------------------------------------------
  113. //        • ~CPict2AsciiApp
  114. // ---------------------------------------------------------------------------------
  115. CPict2AsciiApp::~CPict2AsciiApp()
  116. {
  117. }
  118.  
  119.  
  120. // ---------------------------------------------------------------------------------
  121. //        • StartUp
  122. // ---------------------------------------------------------------------------------
  123. void
  124. CPict2AsciiApp::StartUp()
  125. {
  126. //    ObeyCommand (cmd_Open, nil );
  127. }
  128.  
  129.  
  130. // ---------------------------------------------------------------------------------
  131. //        • FindCommandStatus
  132. // ---------------------------------------------------------------------------------
  133. void
  134. CPict2AsciiApp::FindCommandStatus(
  135.     CommandT    inCommand,
  136.     Boolean        &outEnabled,
  137.     Boolean        &outUsesMark,
  138.     Char16        &outMark,
  139.     Str255        outName )
  140. {
  141.     switch ( inCommand ) {
  142.     
  143.         case cmd_New:
  144.             outEnabled = false;
  145.             break;
  146.  
  147.         case cmd_Preferences:
  148.             outEnabled = true;
  149.             break;
  150.  
  151.         default:
  152.         {
  153.             // Call inherited.
  154.             LDocApplication::FindCommandStatus( inCommand,
  155.                 outEnabled, outUsesMark, outMark, outName );
  156.         }
  157.         break;
  158.  
  159.     }
  160. }
  161.  
  162.  
  163.  
  164. // ---------------------------------------------------------------------------
  165. //        • ObeyCommand
  166. // ---------------------------------------------------------------------------
  167. //    Respond to commands
  168.  
  169. Boolean
  170. CPict2AsciiApp::ObeyCommand(
  171.     CommandT    inCommand,
  172.     void        *ioParam)
  173. {
  174.     Boolean        cmdHandled = true;
  175.  
  176.     switch (inCommand) {
  177.     
  178.         case cmd_Preferences:
  179.             mPreferences.DoDialog (mPreferences.GetAllowed());
  180.             
  181.             // No active menus when back from dialog ?
  182.             // In fact, no target at all !
  183.             SwitchTarget(this);
  184.             break;
  185.  
  186.         case cmd_PageSetup:
  187.             SetupPage();
  188.             break;
  189.             
  190.         default:
  191.             cmdHandled = LDocApplication::ObeyCommand(inCommand, ioParam);
  192.             break;
  193.     }
  194.     
  195.     return cmdHandled;
  196. }
  197.  
  198.  
  199. // ---------------------------------------------------------------------------------
  200. //        • OpenDocument (Called in response to the ODOC event)
  201. // ---------------------------------------------------------------------------------
  202. void
  203. CPict2AsciiApp::OpenDocument(
  204.     FSSpec    *inMacFSSpec)
  205. {
  206.     new CDocument (this, inMacFSSpec, &mPreferences);
  207. }
  208.  
  209.  
  210. // ---------------------------------------------------------------------------------
  211. //        • ChooseDocument (called by the "open" menu item)
  212. // ---------------------------------------------------------------------------------
  213. void
  214. CPict2AsciiApp::ChooseDocument()
  215. {
  216.     // Deactivate the desktop.
  217.     UDesktop::Deactivate();
  218.  
  219.     // Browse for a document.
  220.     SFTypeList theList = { 'PICT' };
  221.     StandardFileReply theReply;
  222.     ::StandardGetFilePreview (nil, 1, theList, &theReply);
  223.  
  224.     // Reactivate the desktop.
  225.     UDesktop::Activate();
  226.  
  227.     // Send an apple event to open the file.    
  228.     if (theReply.sfGood)
  229.         SendAEOpenDoc (theReply.sfFile);
  230.  
  231. }
  232.  
  233.  
  234. // ---------------------------------------------------------------------------------
  235. //        • Additional AppleScript support (Michael Schuerig 97-05-29)
  236. // ---------------------------------------------------------------------------------
  237.  
  238. #define ae_pict2ascii        10000
  239.  
  240. #define pZoomFactor            'zoom'
  241. #define pAutoContrast        'cntr'
  242. #define pOptimizedForPrint    'oprt'
  243. #define pCharSet            'cset'
  244. #define pMonoSpace            'mosp'
  245. #define pAllow8Bit            '8bit'
  246.  
  247.  
  248. // ---------------------------------------------------------------------------------
  249. //        • HandleAppleEvent
  250. // ---------------------------------------------------------------------------------
  251.  
  252. void
  253. CPict2AsciiApp::HandleAppleEvent(
  254.     const AppleEvent    &inAppleEvent,
  255.     AppleEvent            &outAEReply,
  256.     AEDesc                &outResult,
  257.     long                inAENumber)
  258. {
  259.     switch (inAENumber) {
  260.             
  261.         case ae_pict2ascii:
  262.             DoConvertPict2Ascii(inAppleEvent, outAEReply);
  263.             break;
  264.             
  265.         default:
  266.             LDocApplication::HandleAppleEvent(inAppleEvent, outAEReply,
  267.                                 outResult, inAENumber);
  268.             break;
  269.     }
  270. }
  271.  
  272. // ---------------------------------------------------------------------------------
  273. //        • GetAEProperty
  274. // ---------------------------------------------------------------------------------
  275.  
  276. void
  277. CPict2AsciiApp::GetAEProperty(
  278.     DescType        inProperty,
  279.     const AEDesc    &inRequestedType,
  280.     AEDesc            &outPropertyDesc) const
  281. {
  282.     const CPreferences*    thePrefs = &mPreferences;
  283.     OSErr            err;
  284.  
  285.     switch (inProperty) {
  286.         
  287.         case pTextFont: {
  288.             TextTraitsRecord    theTextTraits;
  289.             thePrefs->GetTextTraits(theTextTraits);
  290.             err = ::AECreateDesc(typeChar, &(theTextTraits.fontName)[1], StrLength(theTextTraits.fontName), &outPropertyDesc);
  291.             ThrowIfOSErr_(err);
  292.             break;
  293.             }
  294.         case pTextPointSize: {
  295.             TextTraitsRecord    theTextTraits;
  296.             thePrefs->GetTextTraits(theTextTraits);
  297.             err = ::AECreateDesc(typeShortInteger, &theTextTraits.size, sizeof(theTextTraits.size), &outPropertyDesc);
  298.             ThrowIfOSErr_(err);
  299.             break;
  300.             }
  301.         case pZoomFactor:
  302.             ThrowOSErr_(errAEEventNotHandled); // ### not yet
  303.             break;
  304.             
  305.         case pAutoContrast: {
  306.             Boolean theAutoContrast = thePrefs->GetContrast();
  307.             err = ::AECreateDesc(typeBoolean, &theAutoContrast, sizeof(theAutoContrast), &outPropertyDesc);
  308.             ThrowIfOSErr_(err);
  309.             break;
  310.             }
  311.         case pOptimizedForPrint: {
  312.             Boolean thePrintOpt = !(thePrefs->GetScreen());
  313.             err = ::AECreateDesc(typeBoolean, &thePrintOpt, sizeof(thePrintOpt), &outPropertyDesc);
  314.             ThrowIfOSErr_(err);
  315.             break;
  316.             }
  317.         case pAllow8Bit: {
  318.             Boolean theAllow8Bit = !(thePrefs->Get7Bits());
  319.             err = ::AECreateDesc(typeBoolean, &theAllow8Bit, sizeof(theAllow8Bit), &outPropertyDesc);
  320.             ThrowIfOSErr_(err);
  321.             break;
  322.             }
  323.         case pCharSet: {
  324.             LStr255 theCharSet = thePrefs->GetAllowed();
  325.             err = ::AECreateDesc(typeChar, StringPtr(theCharSet) + 1, theCharSet.Length(), &outPropertyDesc);
  326.             ThrowIfOSErr_(err);
  327.             break;
  328.             }
  329.         case pMonoSpace: {
  330.             Boolean theMonoSpace = thePrefs->GetMonoSpace();
  331.             err = ::AECreateDesc(typeBoolean, &theMonoSpace, sizeof(theMonoSpace), &outPropertyDesc);
  332.             ThrowIfOSErr_(err);
  333.             break;
  334.             }
  335.     
  336.         default:
  337.             LDocApplication::GetAEProperty(inProperty, inRequestedType,
  338.                                             outPropertyDesc);
  339.             break;
  340.     }
  341. }
  342.  
  343.  
  344. // ---------------------------------------------------------------------------------
  345. //        • SetAEProperty
  346. // ---------------------------------------------------------------------------------
  347.  
  348. void
  349. CPict2AsciiApp::SetAEProperty(
  350.     DescType        inProperty,
  351.     const AEDesc&    inValue,
  352.     AEDesc&            outAEReply)
  353. {
  354.     CPreferences*    thePrefs = &mPreferences;
  355.     
  356.     switch (inProperty)
  357.     {
  358.         case pTextFont: {
  359.             Str255                theFontName;
  360.             short                theFontNumber;
  361.             TextTraitsRecord    theTextTraits;
  362.             UExtractFromAEDesc::ThePString(inValue, theFontName);                
  363.             ::GetFNum(theFontName, &theFontNumber);
  364.             if (theFontNumber == 0) {
  365.                 ThrowOSErr_(paramErr);
  366.             }
  367.             else {
  368.                 thePrefs->GetTextTraits(theTextTraits);
  369.                 BlockMoveData(theFontName, theTextTraits.fontName, StrLength(theFontName) + 1);
  370.                 theTextTraits.fontNumber = theFontNumber;
  371.                 thePrefs->SetTextTraits(theTextTraits);
  372. //###                UpdateWindows();
  373.             }
  374.             break;
  375.             }
  376.         case pTextPointSize: {
  377.             short    theFontSize;
  378.             TextTraitsRecord    theTextTraits;
  379.             thePrefs->GetTextTraits(theTextTraits);
  380.             UExtractFromAEDesc::TheInt16(inValue, theFontSize);
  381.             theTextTraits.size = theFontSize;
  382.             thePrefs->SetTextTraits(theTextTraits);
  383. //###                UpdateWindows();
  384.             break;
  385.             }
  386.         case pZoomFactor:
  387.             ThrowOSErr_(errAEEventNotHandled); // ### not yet
  388.             break;
  389.             
  390.         case pAutoContrast: {
  391.             Boolean    theAutoContrast;
  392.             UExtractFromAEDesc::TheBoolean(inValue, theAutoContrast);
  393.             thePrefs->SetContrast(theAutoContrast);
  394. //###                UpdateWindows();
  395.             break;
  396.             }
  397.         case pOptimizedForPrint: {
  398.             Boolean    thePrintOpt;
  399.             UExtractFromAEDesc::TheBoolean(inValue, thePrintOpt);
  400.             thePrefs->SetScreen(!thePrintOpt);
  401. //###                UpdateWindows();
  402.             break;
  403.             }
  404.             
  405.         case pAllow8Bit: {
  406.             Boolean    theAllow8Bit;
  407.             UExtractFromAEDesc::TheBoolean(inValue, theAllow8Bit);
  408.             thePrefs->Set7Bits(!theAllow8Bit);
  409. //###                UpdateWindows();
  410.             break;
  411.             }
  412.  
  413. /* ### not yet
  414.         case pCharSet: {            
  415.             break;
  416.             }
  417.         case pMonoSpace: {
  418.             Boolean    theMonoSpace;
  419.             UExtractFromAEDesc::TheBoolean(inValue, theMonoSpace);
  420.             thePrefs->SetMonoSpace(theMonoSpace);
  421.             break;
  422.             }
  423. */
  424.             
  425.         default:
  426.             LDocApplication::SetAEProperty(inProperty, inValue, outAEReply);
  427.     }
  428. }
  429.  
  430. // ---------------------------------------------------------------------------------
  431. //        • ConvertPict2Ascii
  432. // ---------------------------------------------------------------------------------
  433.  
  434. void
  435. CPict2AsciiApp::DoConvertPict2Ascii(
  436.     const AppleEvent    &inAppleEvent,
  437.     AppleEvent&            outAEReply)
  438. {
  439.     FSSpec        theFSSpec;
  440.     CDocument*    theDoc;
  441.     OSErr        err;
  442.     
  443.     {
  444.         DescType    actualType;
  445.         Size        actualSize;
  446.     
  447.         err = ::AEGetParamPtr(&inAppleEvent, keyDirectObject, typeFSS, &actualType, &theFSSpec, sizeof(theFSSpec), &actualSize);
  448.         ThrowIfOSErr_(err);
  449.     }
  450.     
  451.     theDoc = new CDocument (this, &theFSSpec, &mPreferences, false);    // create an invisible document
  452.     
  453.     Handle theTextH = (theDoc->mGenText).GetText();
  454.     if ( theTextH==nil )
  455.         theTextH = (theDoc->mDynamicText)->GetTextHandle();
  456.  
  457.     {
  458.         StHandleLocker theLock(theTextH);
  459.         err = ::AEPutParamPtr(&outAEReply, keyAEResult, typeChar, *theTextH, ::GetHandleSize(theTextH));
  460.     }
  461.     
  462.     theDoc->Close();
  463.     
  464.     ThrowIfOSErr_(err);
  465. }
  466.